from django import forms
from . import models
from django.core.exceptions import ValidationError
# 用于表单验证,如果从表单处验证，可使用下面的包，最终实现从视图函数处验证，故不使用
# import re
# from django.core.validators import RegexValidator


class LoginForm(forms.Form):
    username = forms.CharField(
        max_length=14,
        widget=forms.TextInput(
            attrs={
                'id': 'username',
                'name': 'username',
                'placeholder': '请输入用户名'
            }
        )
    )
    password = forms.CharField(
        max_length=14,
        widget=forms.PasswordInput(
            attrs={
                'id': 'password',
                'name': 'password',
                'placeholder': '请输入密码'
            }
        )
    )


class RegisterForm(forms.Form):
    username = forms.CharField(
        max_length=14,
        widget=forms.TextInput(
            attrs={
                'type': 'text',
                'id': 'username',
                'placeholder': '请输入用户名'
            }
        )
    )
    password = forms.CharField(
        initial="你的密码",
        max_length=14,
        widget=forms.PasswordInput(
            attrs={
                'type': 'password',
                'id': 'password',
                'name': 'password',
                'placeholder': '请输入密码'
            }
        )
    )

    repassword = forms.CharField(
        initial="重复密码",
        max_length=14,
        widget=forms.PasswordInput(
            attrs={
                'type': 'password',
                'id': 'repassword',  # 修改为不同的ID
                'name': 'repassword',
                'placeholder': '请重复密码'
            }
        )
    )

    nickname = forms.CharField(
        initial="你的昵称",
        max_length=14,
        widget=forms.PasswordInput(
            attrs={
                'type': 'text',
                'id': 'name',
                'name': 'name',
                'placeholder': '请输入昵称'
            }
        )
    )

    email = forms.EmailField(
        widget=forms.EmailInput(
            attrs={
                'type': 'email',
                'id': 'email',
                'placeholder': '请输入邮箱'
            }
        )
    )

    email_code = forms.CharField(
        max_length=6,  # 假设验证码长度为6位
        widget=forms.TextInput(
            attrs={
                'type': 'text',
                'id': 'email_code',
                'name': 'email_code',
                'placeholder': '请输入邮箱验证码'
            }
        )
    )

class UserEditForm(forms.ModelForm):
    class Meta:
        fields = ['username', 'password', 'nickname']  # 根据实际模型字段调整

        widgets = {
            'username': forms.TextInput(attrs={'placeholder': '请输入用户名'}),
            'password': forms.PasswordInput(attrs={'placeholder': '请输入密码'}),
            'nickname': forms.TextInput(attrs={'placeholder': '请输入昵称'}),
        }

    def clean_username(self):
        username = self.cleaned_data['username']
        # 添加验证逻辑，确保用户名的唯一性和格式等要求
        return username

    def clean_password(self):
        password = self.cleaned_data['password']
        # 添加密码格式验证逻辑
        return password

    def clean_nickname(self):
        nickname = self.cleaned_data['nickname']
        # 添加昵称验证逻辑
        return nickname


class PasswordResetForm(forms.Form):
    username = forms.CharField(
        max_length=14,
        widget=forms.TextInput(
            attrs={
                'id': 'username',
                'name': 'username',
                'placeholder': '请输入用户名'
            }
        )
    )
    email = forms.EmailField(label='邮箱')
    password = forms.CharField(
        max_length=14,
        widget=forms.PasswordInput(
            attrs={
                'id': 'password',
                'name': 'password',
                'placeholder': '请输入新密码'
            }
        )
    )
    repassword = forms.CharField(
        max_length=14,
        widget=forms.PasswordInput(
            attrs={
                'id': 'repassword',
                'name': 'repassword',
                'placeholder': '请重复输入新密码'
            }
        )
    )
    email_code = forms.CharField(
        max_length=6,  # 假设验证码长度为6位
        widget=forms.TextInput(
            attrs={
                'type': 'text',
                'id': 'email_code',
                'name': 'email_code',
                'placeholder': '请输入邮箱验证码'
            }
        )
    )
